<template>
  <div class="row justify-between km-tool" v-show="data._id">
    <div class="tread">
      <span>阅读&nbsp;{{ data.statistics.reads }}</span>
    </div>
    <div v-if="data.kmSpaceId !== $store.state.userInfo.kmSpaceId" class="tvote cursor-pointer q-ml-md"
         :class="{voted: voted}" @click="handleVote">
      <i v-show="!voted" class="material-icons q-icon notranslate vote">thumb_up_off_alt</i>
      <i v-show="voted" class="material-icons q-icon notranslate vote voted">thumb_up_alt</i>
      <span>赞&nbsp;{{ data.statistics.votes }}</span>
    </div>
    <!-- <div class="tcomments">
      <span>{{data.statistics.comments}}条评论</span>
    </div> -->
    <div v-if="data.kmSpaceId !== $store.state.userInfo.kmSpaceId && $store.state.userInfo.role !== 99"
         class="tcollects q-ml-md cursor-pointer" @click="handleCollect">
      <span>收藏&nbsp;{{ data.statistics.collects }}</span>
    </div>
    <!-- <div v-if="data.kmSpaceId !== $store.state.userInfo.kmSpaceId && $store.state.userInfo.role !== 99"
       class="tclone  cursor-pointer" @click="handleClone">
      再创作
    </div> -->
    <div class="tshare row  q-mx-md line-height">
      <div>分享至</div>
      <img @click="goWeibo()" class="weibo cursor-pointer" style="margin:10px;width:20px;height:20px"
           src="../../../public/logo/logo-weibo.png" alt=""/>
      <div class="line-height">
        <img class="weixin cursor-pointer" style="margin:8px;width:24px;height:24px"
             src="../../../public/logo/logo-wechat.png" alt=""/>
        <q-tooltip class="bg-white">
          <qrcode-vue :value="pageUrl" :size="120" level="M"/>
        </q-tooltip>
      </div>
    </div>
    <q-space/>
  </div>
</template>

<script>
import QrcodeVue from 'qrcode.vue'
import iContent from 'src/api/content'
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'KmToolbar',
  components: {
    QrcodeVue
  },
  props: {
    data: {
      type: Object
    }
  },
  data () {
    return {
      voted: false,
      voteLoading: false,
      pageUrl: ''
    }
  },
  watch: {
    async data () {
      this.voteHighlight()
    }
  },
  computed: {},
  async mounted () {
    await this.voteHighlight()
    this.pageUrl = location.href
  },
  methods: {
    async handleCollect () {
      let cid = this.data._id
      if (this.data.contentForm === 2) {
        cid = this.data.fromCid
      }
      const res = await iContent.collect({ cid: cid })
      if (res) {
        this.$q.notify({
          icon: 'announcement',
          message: '收藏成功，可在我的个人知识空间查看',
          color: 'info',
          position: 'top',
          timeout: 1500
        })
      }
    },
    async handleClone () {
      const res = await iContent.clone({ cid: this.data._id })
      if (res) {
        const { href } = this.$router.resolve({
          path: '/km/create',
          query: { id: res._id },
          params: { kmSpaceId: res.kmSpaceId }
        })
        console.log(href)
        window.open(href, '_blank')
      }
    },
    async voteHighlight () {
      await this.$store.dispatch('initUserInfo') // TODO 换个方式，请求太频繁了
      // console.log('mounted', this.$store.state.userInfo, this.$route.query.id, this.$store.state.userInfo.contentVote.indexOf(this.$route.query.id))
      if (this.$store.state.km.contentVote.indexOf(this.$route.query.id) >= 0) {
        this.voted = true
      } else {
        this.voted = false
      }
    },
    async handleVote () {
      this.voteLoading = true
      if (this.voted) {
        await this.$store.dispatch('km/popContentVote', this.data)
      } else {
        await this.$store.dispatch('km/addContentVote', this.data)
      }
      //
      this.voted = !this.voted
      this.voteLoading = false
    },
    async goWeibo () {
      if (!this.data) return
      const url = 'https://service.weibo.com/share/share.php' +
          '?appkey=666' +
          `&title=${encodeURIComponent(document.title)} ${this.data.title}  查看详情：` +
          `&url=${this.pageUrl}&r=4` +
          '&searchPic=false&style=simple'
      window.open(url, '__blank')
    }
  }
})

</script>

<style lang="scss">

.line-height {
  line-height: 40px;
}

.km-tool {
  line-height: 40px;
  color: #aaaaaa;
  font-weight: bold;
  background-color: white;
}

.tvote {
  .vote {
    font-size: 16px;
    color: #aaaaaa;
    margin-right: 0;
  }

  .voted {
    color: rgba($c3, 0.85);
  }
}

.voted {
  color: $c3;
}

.tshare {

}

.tcollects {

}
</style>
